<%@include file="/header.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script>
	$
			.ajax({
				type : "GET",
				url : "answerSurvey.do",
				dateType : "json",
				success : function(data) {
					var obj = JSON.parse(data);
					var question = obj.question;
					var choose = obj.choose;
					var nums = new Array();
					var radio = "";
					var check = "";
					var htm = "";
					var sum = 0;
					var id=document.getElementById("id");
					/* var survey = document.getElementById("survey"); */
					var session = document.getElementById("session");
					id.value = session.innerHTML;
					survey.innerHTML = question[0].surveyId;
					var userId='<input type="hidden" name="userId" value='+id.value+'>'
					var surveyId = '<input type="hidden" name="surveyId" value='+question[0].surveyId+'>'
					htm += '<ul class="question" style="list-style-type:none">';
					for (var i = 0; i < question.length; i++) {
						var total = 0;
						for (var j = 0; j < choose.length; j++) {
							if (choose[j].questionId == question[i].questionId) {
								total += 1;
							}
						}
						nums[i] = total;
					}

					for (var i = 0; i < question.length; i++) {
						for (var j = 0; j < nums[i]; j++) {
							if (question[i].questionType == 0) {
								radio += '<li><input type="radio" name="check"><input type="hidden" name="choose" value="">'
										+ choose[j + sum].content + '</li>'
							} else {
								check += '<li><input type="checkbox" name="check"><input type="hidden" name="choose" value="">'
										+ choose[j + sum].content + '</li>'
							}
						}
						sum += nums[i]
					}

					for (var i = 0; i < question.length; i++) {
						var num = i + 1;
						var type = "";
						if (question[i].questionType == 0) {
							type = "单选";
							htm += '<ul style="list-style-type:none">'
									+ num
									+ '.'
									+ question[i].title
									+ type
									+ '</ul><input type="hidden" name="questionId" value='+question[i].questionId+'>'
									+ radio;
						} else {
							type = "多选";
							htm += '<ul style="list-style-type:none">'
									+ num
									+ '.'
									+ question[i].title
									+ type
									+ '</ul><input type="hidden" name="questionId" value='+question[i].questionId+'>'
									+ check;
						}
					}
					htm += '</ul>';
					$("#title").append(userId);
					$("#survey").append(surveyId);
					$("#apitesttry").append(htm);
				}
			});
	function add() {
		var check = document.getElementsByName("check"); 
		var choose = document.getElementsByName("choose");
		 for (var i = 0; i < check.length; i++) {
			if (!check[i].checked) {
				choose[i].value = 0;
			}else{
				choose[i].value = 1;
			}
		} 
	}
	
		
	
</script>
<body >
	  <form method="post"
		action="${pageContext.request.contextPath}/addAnswer.do">  
		<h3 id="title">问卷编号:</h3>
		<h4 id="survey"></h4>
		<div id="apitesttry"></div>
		<input type="submit" onClick="add()">
	  </form> 

</body>
</html>